package mytest.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import test.driver.HttpAPI;
import test.testcase.dao.TestCaseApiDao;
import test.utils.Config;
import test.utils.JsonResult;

@Controller @RequestMapping(value = "/test", produces = "application/json; charset=utf-8")
@CrossOrigin
public class test {
	
	@RequestMapping(value = "/api", method = RequestMethod.POST)
	public @ResponseBody String login(@RequestBody String body) {
		JsonResult result = JsonResult.parse(body);
		String url = result.read("url").toString();
		String url2 = result.read("url2").toString();
		if (("".equals(url) || url == null) && ("".equals(url2) || url2 == null)) {
			return "地址不能为空";
		}
		if ("".equals(url) || url == null) {
			url = url2;
		}
		if (!url.startsWith("http")) {
			return "地址错误";
		}
		String contenttype = result.read("contenttype").toString();
		HttpAPI api = new HttpAPI();
		if (result.read("method").toString().equals("get")) {
			api.setUrl(url);
			try {
				api.get();
				return api.getResponseStr();
			} catch (Exception e) {
				return e.getMessage();
			}
		}
		if (contenttype.equals("form")) {
			for(int i=1;i<100;i++) {
				String key = result.read("key"+i).toString();
				String value = result.read("value"+i).toString();
				if (value == null) {
					value = "";
				}
				if (key != null) {
					api.setParam(key, value);
				}
			}
			api.setContentType(Config.form);
			api.setUrl(url);
			try {
				api.post();
				return api.getResponseStr();
			} catch (Exception e) {
				return e.getMessage();
			}
			
		}
		api.setBody(result.read("body").toString());
		api.setUrl(url);
		try {
			api.post();
			return api.getResponseStr();
		} catch (Exception e) {
			return e.getMessage();
		}
		
	}
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody String save(@RequestBody String body) {
		JsonResult result = JsonResult.parse(body);
		String url = result.read("url").toString();
		String url2 = result.read("url2").toString();
		if (("".equals(url) || url == null) && ("".equals(url2) || url2 == null)) {
			return "地址不能为空";
		}
		if ("".equals(url) || url == null) {
			url = url2;
		}
		if (!url.startsWith("http")) {
			return "地址错误";
		}
		String contenttype = result.read("contenttype").toString();
		String method = result.read("method").toString();
		String bodystr = "";
		if (contenttype.equals("form")) {
			for(int i=1;i<100;i++) {
				String key = result.read("key"+i).toString();
				String value = result.read("value"+i).toString();
				if (value == null) {
					value = "";
				}
				if (key != null) {
					if (bodystr.equals("")) {
						bodystr = key+"="+value;
					}else {
						bodystr = bodystr + "&"+key+"="+value;
					}
				}
			}
		}else {
			bodystr = result.read("body").toString();
		}
		if (method.equals("get")) {
			bodystr = "";
			contenttype = "text";
		}
		
		return TestCaseApiDao.save(url, method, contenttype, bodystr);
	}
	
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	public @ResponseBody String del(@RequestBody String body) {
		JsonResult result = JsonResult.parse(body);
		String url = result.read("url").toString();
		String url2 = result.read("url2").toString();
		if (("".equals(url) || url == null) && ("".equals(url2) || url2 == null)) {
			return "地址不能为空";
		}
		if ("".equals(url) || url == null) {
			url = url2;
		}
		if (!url.startsWith("http")) {
			return "地址错误";
		}
		return TestCaseApiDao.del(url);
	}
	
	@RequestMapping(value = "/get", method = RequestMethod.POST)
	public @ResponseBody String get(@RequestBody String body) {
		JsonResult result = JsonResult.parse(body);
		String url = result.read("url").toString();
		String url2 = result.read("url2").toString();
		if (("".equals(url) || url == null) && ("".equals(url2) || url2 == null)) {
			return "地址不能为空";
		}
		if ("".equals(url) || url == null) {
			url = url2;
		}
		if (!url.startsWith("http")) {
			return "地址错误";
		}
		return TestCaseApiDao.get(url);
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public @ResponseBody String list() {
		return TestCaseApiDao.list();
	}
	
}
